#!/bin/bash
# Author: zhengxuye
# Copyright Huawei Technologies Co., Ltd. 2010-2018. All rights reserved.
set -e

######################
# 构建iso镜像
# Globals:
# Arguments:
# Returns:
######################
function make_devstation_iso()
{
    chroot_run "cd /home; bash make_version.sh make_devstation_iso_inchroot"
}
######################
# 在chroot中构建iso镜像
# Globals:
# Arguments:
# Returns:
######################
function make_devstation_iso_inchroot()
{
    get_version
    release_dir=$(get_release_dir)
    TIME_DIR="${release_dir#${HTTP_DIR}}"
    TIME=${TIME_DIR##*/}
    TIME=${TIME#"${version}"-}
    yum_conf="${BUILD_SCRIPT_DIR}/config/repo_conf/repofile.conf"
    yum clean all -c "${yum_conf}"
    if rpm -q oemaker &> /dev/null; then
        yum remove oemaker -y
    fi
    if rpm -q lorax &> /dev/null; then
        yum remove lorax -y
    fi
    yum install oemaker lorax -y -c "${yum_conf}"
    cd /opt/oemaker
    #sed -i "/^%pre/i repo --name=\"EPOL\" --baseurl=${EPOL_PROJECT_REPO}\nrepo --name=\"other\" --baseurl=${THIRD_REPO}" config/$ARCH/livecd/livecd_$ARCH.ks
    sed -i "/^%pre/i repo --name=\"everything\" --baseurl=http://121.36.84.172/dailybuild/EBS-openEuler-24.09/rc7_openeuler-2024-09-28-08-34-02/everything/${ARCH}\nrepo --name=\"epol\" --baseurl=http://121.36.84.172/dailybuild/EBS-openEuler-24.09/rc7_openeuler-2024-09-28-08-34-02/EPOL/main/${ARCH}" config/$ARCH/livecd/livecd_$ARCH.ks

    sed -i 's/part \/ --fstype="ext4" --size=40000/part \/ --fstype="ext4" --size=4000/' config/$ARCH/livecd/livecd_$ARCH.ks
    sed -i '/%post/a \
touch /etc/yum.repos.d/local.repo\n\
cat << EOF > /etc/yum.repos.d/local.repo\n\
[local-repo]\n\
name=local\n\
baseurl=file:///run/initramfs/live\n\
enabled=1\n\
gpgcheck=0\n\
EOF\n\
echo "LANG=zh_CN.UTF-8" > /etc/locale.conf\n\
systemctl enable calamares\n\
' config/$ARCH/livecd/livecd_$ARCH.ks

    # 删除rd.live.ram参数
    if [ "$ARCH" == "x86_64" ]; then
        sed -i "/append initrd=initrd.img.*rd.live.ram.*/s/rd\.live\.ram //" config/$ARCH/livecd/live/config_files/$ARCH/isolinux.cfg
        sed -i "/kernel.*rd\.live\.ram.*/s/rd\.live\.ram //" config/$ARCH/livecd/live/config_files/$ARCH/grub.conf
        sed -i "/linuxefi.*rd\.live\.ram.*/s/rd\.live\.ram //" config/$ARCH/livecd/live/config_files/$ARCH/grub2-efi-lockdown.cfg
        sed -i "/linuxefi.*rd\.live\.ram.*/s/rd\.live\.ram //" config/$ARCH/livecd/live/config_files/$ARCH/grub2-efi.cfg
    else
        sed -i "/kernel.*rd\.live\.ram.*/s/rd\.live\.ram //" config/$ARCH/livecd/live/config_files/$ARCH/grub.conf
        sed -i "/linux.*rd\.live\.ram.*/s/rd\.live\.ram //" config/$ARCH/livecd/live/config_files/$ARCH/grub2-efi.cfg
    fi
    echo -e "\nwqy-zenhei-fonts\ncalamares\ncalamares-devel\ncalamares-interactiveterminal\ncalamares-libs\ncalamares-plasmalnf\nkpmcore\nkpmcore-devel" >> config/$ARCH/livecd/rpmlist
    sed -i '/python3-bind/d' config/$ARCH/livecd/rpmlist
    cat config/$ARCH/livecd/livecd_$ARCH.ks
    REPOS=`echo "${THIRD_REPO}" | sed 's/[ \t]*$//g'`
    set +e
    num=0
    set +u
    while [ "${num}" -lt 3 ]
    do
        bash -x oemaker -t livecd -p ${PRODUCTS} -v "${OS_VERSION}" -r "" -s "${REPOS}"
        if [ $? -eq 0 ];then
            break
        elif [ $? -eq 133 ]; then
            sleep 60
            ((num=num+1))
            continue
        else
            log_error "make_iso fail"
            break
        fi
    done
    if [ "${num}" -ge 3 ]; then
        log_error "retry make_iso fail"
    fi
    
    cd /result
    livecd_iso=$(ls *.iso)
    mkdir -p /mnt/iso /mnt/iso_cp
    mount $livecd_iso /mnt/iso
    cp -rf /mnt/iso/* /mnt/iso_cp/
    umount /mnt/iso

    volume_id=$(isoinfo -d -i $livecd_iso | grep "Volume id" | awk '{print $NF}')

    mkdir /mnt/iso_cp/Packages
    mkdir -p /tmp/intallroot
    if [ "$ARCH" == "x86_64" ]; then
        yumdownloader --destdir=/mnt/iso_cp/Packages/ webp-pixbuf-loader yum code checkpolicy iproute dhcp openssh grub2 kernel bash coreutils grub2-pc passwd sudo xorg-* dejavu-fonts liberation-fonts gnu-*-fonts google-*-fonts vulkan adwaita-icon-theme atk atkmm at-spi2-atk at-spi2-core baobab abattis-cantarell-fonts cheese clutter clutter-gst3 clutter-gtk cogl dconf dconf-editor devhelp eog epiphany evince evolution-data-server file-roller folks gcab gcr gdk-pixbuf2 gdm gedit geocode-glib gfbgraph gjs glib2 glibmm24 glib-networking gmime30 gnome-autoar gnome-backgrounds gnome-bluetooth gnome-builder gnome-calculator gnome-calendar gnome-characters gnome-clocks gnome-color-manager gnome-contacts gnome-control-center gnome-desktop3 gnome-disk-utility gnome-font-viewer gnome-getting-started-docs gnome-initial-setup gnome-keyring gnome-logs gnome-menus gnome-music gnome-online-accounts gnome-online-miners gnome-photos gnome-remote-desktop gnome-screenshot gnome-session gnome-settings-daemon gnome-shell gnome-shell-extensions gnome-software gnome-system-monitor gnome-terminal gnome-tour gnome-user-docs gnome-user-share gnome-video-effects gnome-weather gobject-introspection gom grilo grilo-plugins gsettings-desktop-schemas gsound gspell gssdp gtk3 gtk4 gtk-doc gtkmm30 gtksourceview4 gtk-vnc2 gupnp gupnp-av gupnp-dlna gvfs json-glib libchamplain libdazzle libgdata libgee libgnomekbd libgsf libgtop2 libgweather libgxps libhandy libmediaart libnma libnotify libpeas librsvg2 libsecret libsigc++20 libsoup mm-common mutter nautilus orca pango pangomm libphodav python3-pyatspi python3-gobject rest rygel simple-scan sushi sysprof tepl totem totem-pl-parser tracker3 tracker3-miners vala vte291 yelp yelp-tools yelp-xsl zenity devstation-config --alldeps --resolve -c "${yum_conf}"
    else
        yumdownloader --destdir=/mnt/iso_cp/Packages/ webp-pixbuf-loader yum code checkpolicy iproute dhcp openssh grub2-efi shim kernel bash coreutils efibootmgr passwd sudo xorg-* dejavu-fonts liberation-fonts gnu-*-fonts google-*-fonts vulkan adwaita-icon-theme atk atkmm at-spi2-atk at-spi2-core baobab abattis-cantarell-fonts cheese clutter clutter-gst3 clutter-gtk cogl dconf dconf-editor devhelp eog epiphany evince evolution-data-server file-roller folks gcab gcr gdk-pixbuf2 gdm gedit geocode-glib gfbgraph gjs glib2 glibmm24 glib-networking gmime30 gnome-autoar gnome-backgrounds gnome-bluetooth gnome-builder gnome-calculator gnome-calendar gnome-characters gnome-clocks gnome-color-manager gnome-contacts gnome-control-center gnome-desktop3 gnome-disk-utility gnome-font-viewer gnome-getting-started-docs gnome-initial-setup gnome-keyring gnome-logs gnome-menus gnome-music gnome-online-accounts gnome-online-miners gnome-photos gnome-remote-desktop gnome-screenshot gnome-session gnome-settings-daemon gnome-shell gnome-shell-extensions gnome-software gnome-system-monitor gnome-terminal gnome-tour gnome-user-docs gnome-user-share gnome-video-effects gnome-weather gobject-introspection gom grilo grilo-plugins gsettings-desktop-schemas gsound gspell gssdp gtk3 gtk4 gtk-doc gtkmm30 gtksourceview4 gtk-vnc2 gupnp gupnp-av gupnp-dlna gvfs json-glib libchamplain libdazzle libgdata libgee libgnomekbd libgsf libgtop2 libgweather libgxps libhandy libmediaart libnma libnotify libpeas librsvg2 libsecret libsigc++20 libsoup mm-common mutter nautilus orca pango pangomm libphodav python3-pyatspi python3-gobject rest rygel simple-scan sushi sysprof tepl totem totem-pl-parser tracker3 tracker3-miners vala vte291 yelp yelp-tools yelp-xsl zenity devstation-config --alldeps --resolve -c "${yum_conf}"
    fi
    cd /result
    mkdir /mnt/iso_cp/repodata
    cp /opt/oemaker/config/${ARCH}/normal.xml /mnt/iso_cp/repodata/
    createrepo -d -g /mnt/iso_cp/repodata/normal.xml /mnt/iso_cp
    iso_name="${PRODUCTS}-${OS_VERSION}-DevStation-${ARCH}-dvd.iso"

    if [ "$ARCH" == "x86_64" ]; then
        mkisofs -R -J -T -r -l -d -joliet-long -allow-multidot -allow-leading-dots -no-bak -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e images/efiboot.img -no-emul-boot -V "$volume_id" -o /result/${iso_name} /mnt/iso_cp
    else
        mkisofs -R -J -T -r -l -d -joliet-long -allow-multidot -allow-leading-dots -no-bak -V "${volume_id}" -o /result/${iso_name} -e images/efiboot.img -no-emul-boot /mnt/iso_cp
    fi
    set -e
    cd "/result"

    TGZ_NAME=$(ls ${iso_name})
    if [ x"${TGZ_NAME}" == x'' ]; then  log_error "can not find iso";fi
    create_checksum "${TGZ_NAME}"
    iso_rpmlist="${OS_NAME}-${OS_VERSION}-DevStation-${ARCH}.rpmlist"
    mkdir temp && mount *"${ARCH}"-dvd.iso temp
    cd temp/Packages
    ls *.rpm > "../../${iso_rpmlist}"
    cd ../..
    umount temp
    [ -n temp ] && rm -rf temp
    RELEASE_DIR="${release_dir}/devStation/$ARCH"
    SSH_CMD="mkdir -p ${RELEASE_DIR}"
    sshcmd "${SSH_CMD}"
    sshscp "${TGZ_NAME} ${TGZ_NAME}${SHA256SUM} ${iso_rpmlist}" "${RELEASE_DIR}"
}
